package com.zzx.magic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzx.magic.entity.BpRecommend;
import com.zzx.magic.entity.BusinessPlan;
import com.zzx.magic.mapper.BpRecommendMapper;
import com.zzx.magic.mapper.BusinessPlanMapper;
import com.zzx.magic.service.IBusinessPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author ZhangXx
 * @since 2024-06-12
 */
@Service
public class BusinessPlanServiceImpl extends ServiceImpl<BusinessPlanMapper, BusinessPlan> implements IBusinessPlanService {

    @Autowired
    BusinessPlanMapper businessPlanMapper;

    @Autowired
    BpRecommendMapper bpRecommendMapper;


    /*
        增加BP
     */
    @Override
    public Integer addBp(BusinessPlan bp) {
        return businessPlanMapper.insert(bp);
    }

    /*
        修改BP
    */
    @Override
    public Integer updateBp(BusinessPlan bp) {
        return businessPlanMapper.updateById(bp);
    }

    /*
        查看BP
     */
    @Override
    public BusinessPlan selectBp(String id) {
        BusinessPlan businessPlan = businessPlanMapper.selectById(id);
        return businessPlan;
    }

    /*
        删除BP
    */
    @Override
    public Integer deleteBp(String id) {
        return businessPlanMapper.deleteById(id);
    }

    /*
        模糊查询
     */
    @Override
    public List<BusinessPlan> queryList(String keword, String type) {
        if (type.equals(0)) {
            QueryWrapper<BusinessPlan> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("bp_company", keword);
            return businessPlanMapper.selectList(queryWrapper);
        }
        return null;
    }

    /*
        首页项目推荐
     */
    @Override
    public List<BusinessPlan> recommend(String type) {
        List<BpRecommend> recommends = bpRecommendMapper.selectList(new QueryWrapper<BpRecommend>().eq("type", type));
        List<Integer> objects = Collections.emptyList();
        for (BpRecommend entity : recommends) {
            objects.add(entity.getBpId());
        }
        return businessPlanMapper.selectBatchIds(objects);

    }

    @Override
    public List<BusinessPlan> tagRecommend(String tag) {
         return businessPlanMapper.selectList(new QueryWrapper<BusinessPlan>().like("bp_tags", tag));
    }

}
